//
//  ViewController.swift
//  WebViewJavaScript
//
//  Created by DKHS on 2018/1/9.
//  Copyright © 2018年 CBL. All rights reserved.
//

import UIKit
import Alamofire
import Kingfisher
import WebViewJavascriptBridge
import CocoaLumberjack

class ViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var actionButton: UIButton!
    
    /// <#注释#>
    fileprivate lazy var bridge: WebViewJavascriptBridge = {
        let bridge = WebViewJavascriptBridge(webView)!
        bridge.setWebViewDelegate(self)
        return bridge
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        WebViewJavascriptBridge.enableLogging()
        if let filePath = Bundle.main.path(forResource: "local", ofType: "html") {
            if let htmlString = try? String(contentsOfFile: filePath) {
                webView.loadHTMLString(htmlString, baseURL: URL(fileURLWithPath: filePath))
            }
        }
        bridge.registerHandler("ObjC Echo") { [weak self] (data, responseCallBack) in
            print("JS执行Native后,JS回传给Native: \(String(describing: data))")
            responseCallBack?("JS执行Native后,Native收到JS的回传后,Native回传给JS")
            self?.actionButton.setTitle(data as? String, for: .normal)
        }
        
        DDLog.add(DDTTYLogger.sharedInstance, with: DDLogLevel.all)
        DDLog.add(DDTTYLogger.sharedInstance)
        DDLog.add(DDASLLogger.sharedInstance)
        
        let fileLogger: DDFileLogger = DDFileLogger()
        fileLogger.rollingFrequency = TimeInterval(60 * 60 * 24)
        fileLogger.logFileManager.maximumNumberOfLogFiles = 7
        DDLog.add(fileLogger)
        
        DDLogVerbose("Verbose")
        DDLogDebug("Debug")
        DDLogInfo("Info")
        DDLogWarn("Warn")
        DDLogError("Error")
        
        let shareUserDefaults = UserDefaults(suiteName: "group.WebViewJavaScript.CblToady")
        shareUserDefaults?.set("沧海一声笑", forKey: "CblTodayKey")
        shareUserDefaults?.synchronize()
        
        
        if var containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.WebViewJavaScript.CblToady") {
            containerURL.appendPathComponent("Library/Caches/test")
            try? "书剑恩仇录".write(to: containerURL, atomically: true, encoding: String.Encoding.utf8)
        }
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    @IBAction func didClickActionButton(_ sender: UIButton) {
        bridge.callHandler("JS Echo", data:"Native执行JS,Native传给JS") { [weak self] (responseData) in
            print("Native执行JS后,JS回传给Native: \(String(describing: responseData))")
            self?.actionButton.setTitle(responseData as? String, for: .normal)
        }
    }
    
}

// MARK: - UIWebViewDelegate
extension ViewController: UIWebViewDelegate {
    func webViewDidStartLoad(_ webView: UIWebView) {
        
    }
    func webViewDidFinishLoad(_ webView: UIWebView) {
        
    }
}

